import store from '@/store'
// 创建permission插件
export default {
  install(Vue) {
    // 创建 v-permission指令
    Vue.directive('permission', {
      // 指令所在元素被插入页面时
      inserted(el, binding) {
        const { value } = binding
        const roles = store.getters && store.getters.roles
        console.log(roles)
        console.log(value.length)
        if (value && value instanceof Array) {
          if (value.length > 0) {
            const permissionRoles = value
            const hasPermission = roles.some(role => {
              console.log(role)
              console.log(permissionRoles)
              console.log(permissionRoles.includes(role))
              return permissionRoles.includes(role)
            })
            if (!hasPermission) {
              el.parentNode && el.parentNode.removeChild(el)
            }
          }
        } else {
          throw new Error(`使用方式： v-permission="['admin','editor']"`)
        }
      }
    })
  }
}

